#!/usr/bin/env python

import optparse
import os
import sys

import minuit
import numpy
import matplotlib
from matplotlib import pyplot
import tables

import histogram
from charmicat import BadCommandLineError

import ypipi
from ypipi import dafit


def main():
    '''
    ypipi_fit_Y2S.py [opts] signal.hpkl signal_w_bg.hpkl

    Fit histogram `mm_off_pi0pi0' from 9.35 to 9.7 GeV with Gauss + PolN
    '''

    op = optparse.OptionParser(usage=main.__doc__)

    opts, args = op.parse_args()

    if len(args) < 2:
        msg = 'Please supply a signal and signal with BG histogram file.'
        raise BadCommandLineError(msg, -1)

    m_fixed_sig, initial_values, chi2i, chi2f = dafit.fixed_sig_fit(args[1])
    
    dafit.plot_fit_bg_subtracted(args[1], m_fixed_sig, initial_values, chi2i, chi2f)

    return 0
    
    

if __name__ == "__main__":
    try:
        sys.exit(main())
    except BadCommandLineError, e:
        import os
        sys.stderr.write(str(e) + "\n")
        sys.stderr.write("See `{0} -h' for more.\n".format(os.path.basename(sys.argv[0])))
        sys.stderr.flush()
        sys.exit(e.retcode)
